package org.zavgame.ejb;

import javax.annotation.PostConstruct;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

/**
 * OpenEJB interceptor for statistic request time data.
 * 
 * @author alexey
 *
 */
public class PlayerStatInterceptor
{

   /**
    * Collect statistic data on every request.
    * Store request time.
    * 
    * @param ic
    * @return
    * @throws Exception
    */
   @AroundInvoke
   protected Object businessMethodInterceptor(InvocationContext ic) throws Exception
   {
      long startTime = System.currentTimeMillis();
      try
      {
         return ic.proceed();
      }
      finally
      {
         long time = System.currentTimeMillis() - startTime;
         StatisticRegistry.getInstance().add(time);
      }
   }

   @PostConstruct
   protected void postConstructInterceptor(InvocationContext ic) throws Exception
   {
      ic.proceed();
   }
}
